gadget: Handle visibility
authorBenjamin Otte <otte@redhat.com>
Wed, 23 Dec 2015 00:50:20 +0000 (01:50 +0100)
committerCosimo Cecchi <cosimoc@gnome.org>
Tue, 29 Dec 2015 21:48:58 +0000 (13:48 -0800)
As GtkCssNode has the visibility concept, it makes sense to mirror it in
gadgets.

Do what visibility does in widgets: Hidden gadgets can't be drawn or
allocated and request a 0x0 size.

Note that just like widgets, gadget visibility must not be changed in
size request, allocate or draw handlers.

GtkWidget::child-visible has no equivalent yet, code will have to
emulate that manually.

gtk/gtkcssgadget.c
gtk/gtkcssgadgetprivate.h

index 7f8abb0e2d53d87aaf5b0e8cfe71b1acffd7e3c7..18d9370914eb0f1fda6fbb265cb665a01ac9071a 100644 (file)
@@ -337,6 +337,23 @@ gtk_css_gadget_get_owner (GtkCssGadget *gadget)
   return priv->owner;
 }
 
+void
+gtk_css_gadget_set_visible (GtkCssGadget *gadget,
+                            gboolean      visible)
+{
+  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);
+
+  gtk_css_node_set_visible (priv->node, visible);
+}
+
+gboolean
+gtk_css_gadget_get_visible (GtkCssGadget *gadget)
+{
+  GtkCssGadgetPrivate *priv = gtk_css_gadget_get_instance_private (gadget);
+
+  return gtk_css_node_get_visible (priv->node);
+}
+
 /**
  * gtk_css_gadget_add_class:
  * @gadget: a #GtkCssGadget
@@ -456,6 +473,17 @@ gtk_css_gadget_get_preferred_size (GtkCssGadget   *gadget,
   if (natural == NULL)
     natural = &unused_natural;
 
+  if (!gtk_css_gadget_get_visible (gadget))
+    {
+      *minimum = 0;
+      *natural = 0;
+      if (minimum_baseline)
+        *minimum_baseline = -1;
+      if (natural_baseline)
+        *natural_baseline = -1;
+      return;
+    }
+
   style = gtk_css_gadget_get_style (gadget);
   get_box_margin (style, &margin);
   get_box_border (style, &border);
@@ -531,6 +559,9 @@ gtk_css_gadget_allocate (GtkCssGadget        *gadget,
 
   g_return_if_fail (out_clip != NULL);
 
+  if (!gtk_css_gadget_get_visible (gadget))
+    return;
+
   priv->allocated_size = *allocation;
   priv->allocated_baseline = baseline;
 
@@ -609,6 +640,9 @@ gtk_css_gadget_draw (GtkCssGadget *gadget,
   int x, y, width, height;
   int contents_x, contents_y, contents_width, contents_height;
 
+  if (!gtk_css_gadget_get_visible (gadget))
+    return;
+
   x =  priv->allocated_size.x;
   y =  priv->allocated_size.y;
   if (priv->owner && !gtk_widget_get_has_window (priv->owner))
@@ -694,6 +728,15 @@ gtk_css_gadget_get_border_allocation (GtkCssGadget  *gadget,
 
   g_return_if_fail (GTK_IS_CSS_GADGET (gadget));
 
+  if (!gtk_css_gadget_get_visible (gadget))
+    {
+      if (allocation)
+        allocation->x = allocation->y = allocation->width = allocation->height = 0;
+      if (baseline)
+        *baseline = -1;
+      return;
+    }
+
   get_box_margin (gtk_css_gadget_get_style (gadget), &margin);
 
   if (allocation)
@@ -723,6 +766,15 @@ gtk_css_gadget_get_content_allocation (GtkCssGadget  *gadget,
 
   g_return_if_fail (GTK_IS_CSS_GADGET (gadget));
 
+  if (!gtk_css_gadget_get_visible (gadget))
+    {
+      if (allocation)
+        allocation->x = allocation->y = allocation->width = allocation->height = 0;
+      if (baseline)
+        *baseline = -1;
+      return;
+    }
+
   style = gtk_css_gadget_get_style (gadget);
   get_box_margin (style, &margin);
   get_box_border (style, &border);
index 551064fde6a330c4480d0dd1b10d17fc16dbee0d..b23f5e738639bbe4a40d15327460e17b0f8c08a1 100644 (file)
@@ -78,6 +78,10 @@ GtkCssNode *    gtk_css_gadget_get_node                 (GtkCssGadget
 GtkCssStyle *   gtk_css_gadget_get_style                (GtkCssGadget           *gadget);
 GtkWidget *     gtk_css_gadget_get_owner                (GtkCssGadget           *gadget);
 
+void            gtk_css_gadget_set_visible              (GtkCssGadget           *gadget,
+                                                         gboolean                visible);
+gboolean        gtk_css_gadget_get_visible              (GtkCssGadget           *gadget);
+
 void            gtk_css_gadget_add_class                (GtkCssGadget           *gadget,
                                                          const char             *name);
 void            gtk_css_gadget_remove_class             (GtkCssGadget           *gadget,